﻿using _15EfCore反向工程.Context;
using _15EfCore反向工程.Migrations;
using Microsoft.EntityFrameworkCore;

namespace _15EfCore反向工程;

class Program
{
    static void Main(string[] args)
    {
        using MyDbContext mdc = new MyDbContext();
        // var myBook = mdc.TBooks.Single(b => b.Id == 15);
        // Console.WriteLine($"{myBook.Title},{myBook.Author},{myBook.Price}");
        
        foreach (var book in QueryDynamic(null,null,20,1))
        {
            Console.WriteLine($"{book.Title},{book.Price},{book.Author}");
        }
    }

    static TBook[] QueryDynamic(string Title, double? lowPrice, double? TopPrice, int orderByType)
    {
        using MyDbContext mdc = new MyDbContext();
        IQueryable<TBook> books = mdc.TBooks;
        if (!string.IsNullOrEmpty(Title))
        {
            books = books.Where(b => b.Title == Title);
        }

        if (lowPrice != null)
        {
            books = books.Where(b => b.Price >= lowPrice);
        }

        if (TopPrice != null)
        {
            books = books.Where(b => b.Price <= TopPrice);
        }

        switch (orderByType)
        {
            case 1:
                books = books.OrderByDescending(b => b.Price); 
                break;
            case 2:
                books = books.OrderBy(b => b.Price);
                break;
        }

        return books.ToArray();
    }
}